<script>
    var page_list = "supplier_list";
    $(function() {
        var tenantCode="";
                    showLoading();
                    fy_api.ajax({
                        url: "smsSetting/getTenantCode.do?_t="+new Date().getTime(),
                        type:"POST",
                        data: "",
                        success: function(data){
                            if(data.success){
                                tenantCode = data.message;
                    $("body").layout();
                    grid_init($("#grid"),{
                        singleSelect: false,
                        columns:[[
                            { field: 'o.id', checkbox: true, title: 'ID'},
                            { field: 'o.tenant_code', title: '租户标识', width: 120},
                            { field: 'o.supplier_name', title: '供应商简称', width: 120},
                            { field: 'o.supplier_full_name', title: '供应商全称', width: 160},
                            { field: 'o.app_platform', title: '应用平台'},
                            { field: 'o.is_used', title: '是否启用',_sql:"case when o.is_used='Y' then '正常' else '不启用' end",formatter: function(value,row,index){
                                if (value=="Y"){
                                    return "正常";
                                } else{
                                    return "<span style='color:red'>不启用</span>";
                                }
                            }},
                            { field: 'o.view_sort', title: '展示排序顺序'},
                            { field: 'o.create_time', title: '创建时间'},
                            { field: 'o.update_time', title: '修改时间'}]],
                        sid: 'o.id',
                        _page: page_list,
                        _table: " sms_supplier o where o.tenant_code in("+tenantCode+")",
                        _exportTitle: "供应商信息列表",
                        _context_handle: [{
                            text: "详情",
                            icon: "icon-info",
                            handle: function(index,row){
                                j_showWin({url:'supplier/info.html?keyId='+this.getFieldValue(row,"o.id"),title:'供应商信息',iconCls: 'icon-form-info'})
                            }
                        },{
                            text: "编辑",
                            auth: "update",
                            icon: "icon-edit",
                            handle: function(index,row){
                                j_showWin({url:'supplier/edit.html?keyId='+this.getFieldValue(row,"o.id"),title:'编辑供应商信息',iconCls: 'icon-form-edit'})
                            }
                        },{
                            text: "删除",
                            auth: "delete",
                            icon: "icon-config-delete",
                            handle: function(index,row){
                                var grid = $(this);
                                var self = this;
                                $.messager.confirm('系统提示', '您确认删除该项信息"'+self.getFieldValue(row,"o.supplier_name")+'"?', function(r){
                                    if (r){
                                        grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                                        showLoading();
                                        fy_api.ajax({
                                            url: "supplier/delete.do?_t="+new Date().getTime(),
                                            data: {"keyId":self.getFieldValue(row,"o.id")},
                                            success: function(data){
                                                hideLoading();
                                                if(data.success){
                                                    j_tip(data.message,'success');
                                                    grid.datagrid("reload");
                                                }else{
                                                    $.messager.alert('系统提示',data.message,'error',function(){
                                                        grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                                                    });
                                                }
                                            }
                                        });
                                    }else{
                                        grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                                    }
                                });
                            }
                        }],
                        toolbar: '#grid_toolbar',
                        searchDiv: '#grid_search'
                    });
                }
            }
        });

        $("#btnBatchDelete").click(function(){
            var grid = grid_get("grid");
            if(grid && grid != null){
                var rows = grid.datagrid("getChecked");
                if(rows == null || rows.length == 0){
                    j_tip("请先至少勾选一行数据!","error");
                    return;
                }
                var keyIds = "";
                var self = grid[0];
                var list = [];
                for(var i=0;i<rows.length;i++){
                    var keyId = self.getFieldValue(rows[i],"o.id");
                    list.push({id:keyId,name:self.getFieldValue(rows[i],"o.id")});
                    keyIds += (keyIds==""?"":",")+keyId;
                }
                $.messager.confirm('系统提示', '您确认删除选中的'+rows.length+'条数据?', function(r){
                    if (r){
                        grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                        showLoading();
                        fy_api.ajax({
                            type:"POST",
                            url: "supplier/batchDelete.do?_t="+new Date().getTime(),
                            data: {"keyIds":keyIds},
                            success: function(data){
                                hideLoading();
                                if(data.object==null || data.object.length==0){
                                    $.messager.alert('系统提示',"删除保存成功,共"+rows.length+"行",'success',function(){
                                        grid.datagrid("reload");
                                    });
                                }else if(data.object!=null && data.object.length>0){
                                    var html = "";
                                    for(var i=0;i<data.object.length;i++){
                                        var id = data.object[i][0];
                                        for(var j=0;j<list.length;j++){
                                            if(list[j].id==id){
                                                html += "<br/>\""+list[j].name+"\":"+data.object[i][1];
                                                break;
                                            }
                                        }
                                    }
                                    if(data.object.length == rows.length){
                                        $.messager.alert('系统提示',"删除保存失败,共"+rows.length+"行:"+html,'hide',function(){
                                            grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                                        });
                                    }else{
                                        $.messager.alert('系统提示',"部分删除成功,成功"+(rows.length-data.object.length)+"行,失败"+data.object.length+"行:"+html,'hide',function(){
                                            grid.datagrid("reload");
                                        });
                                    }
                                }
                            }
                        });
                    }else{
                        grid.datagrid('getPanel').panel('panel').find("a:first").focus();
                    }
                });
            }
        })

    });
</script>
<div data-options="region:'center',title:false,border:false" id="divCenter" style="">
    <table id="grid"></table>
</div>
<div id="grid_toolbar" style="display:none;padding:3px;">
    <a href="javascript:void(0)" class="easyui-linkbutton auth-button" action="add" data-options="plain:true,iconCls:'icon-config-add'" onclick="j_showWin({url:'supplier/edit.html',title:'添加供应商信息',iconCls: 'icon-form-add'})">添加</a>
    <a href="javascript:void(0)" class="easyui-linkbutton auth-button" action="delete" data-options="plain:true,iconCls:'icon-config-delete'" id="btnBatchDelete">批量删除</a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'layout-button-up'" onclick="var button=$(this);if(button.linkbutton('options').iconCls=='layout-button-up'){$('body').layout('collapse','north');button.linkbutton({iconCls:'layout-button-down'})}else{$('body').layout('expand','north');button.linkbutton({iconCls:'layout-button-up'})}button.linkbutton('disable');setTimeout(function(){button.linkbutton('enable');},200)" style="float:right"></a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-help'" id="help" style="float:right"></a>
    <a href="javascript:void(0)" class="easyui-linkbutton" data-options="plain:true,iconCls:'icon-search'" onclick="grid_get('grid')[0].search()" style="float:right">刷新</a>
</div>